package com.example.noteshare;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

import com.example.noteshare.Utilities.Constants;
import com.example.noteshare.Utilities.Repo;
import com.example.noteshare.asyncTasks.AsyncDownload;
import com.example.noteshare.asyncTasks.AsyncGet;
import com.example.noteshare.gson.AccountCreationResult;
import com.example.noteshare.gson.MyFile;
import com.example.noteshare.gson.MyFilesResult;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MyFilesPage extends Activity {
	
	private List<MyFile> myFilesList;
	String fileSelected;
	final Context context = this;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_files_page);
		
		myFilesList = new ArrayList<MyFile>();
		ListView myFiles = (ListView) findViewById(R.id.lstMyFiles);
		
		Map<String, String> params = new HashMap<String, String>();
		params.put("id", Repo.userId);
		
		AsyncGet async = new AsyncGet(Constants.serverIP + Constants.myFiles, params);
		String result = "{}";
		try {
			result = async.execute().get();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//parse tje results
		Gson gson = new GsonBuilder().create();
		MyFilesResult acr = gson.fromJson(result, MyFilesResult.class);
		
		//ArrayAdapter<String> myFilesAdapater = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results);
		ArrayList<String> results = new ArrayList<String>();
		for(MyFile file : acr.getMyFiles()){
			myFilesList.add(file);
			results.add(file.getName());
		}
		ArrayAdapter<String> myFilesAdapater = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results);
		myFiles.setAdapter(myFilesAdapater);
		myFilesAdapater.setNotifyOnChange(true);
		
		myFiles.setOnItemClickListener(new AdapterView.OnItemClickListener()
		{
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int pos, long id)
			{
				fileSelected =(String) parent.getItemAtPosition(pos); // name of file selected
				final MyFile fileId = myFilesList.get(pos);
		        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
		 
					// set title
					alertDialogBuilder.setTitle("Download");
		 
					alertDialogBuilder
						.setMessage("Download Notes from NoteShare?")
						.setCancelable(false)
						.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,int id) {
								Map<String, String> params = new HashMap<String, String>();
								params.put("userId", Repo.userId);
								params.put("fileId", fileId.getId());
								AsyncDownload async = new AsyncDownload(Constants.serverIP + Constants.getFile, params, "mnt/sdcard/", getApplicationContext());
								async.execute();
							}
						})
						.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,int id) {
								// if this button is clicked, just close
								// the dialog box and do nothing
								dialog.cancel();
							}
						});
		 
						// create alert dialog
						AlertDialog alertDialog = alertDialogBuilder.create();
		 
						// show it
						alertDialog.show();

			}
		});
		

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.my_files_page, menu);
		return true;
	}

}
